package com.woniu.cms.psm.core.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.woniu.cms.psm.core.entity.Message;
import com.woniu.cms.psm.core.service.IMessageService;
import com.woniu.common.api.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lilili
 * @since 2022-09-23
 */
@RestController
@RequestMapping("/message")
@Api(tags = "通知管理器")
public class MessageController {
    @Resource
    private IMessageService messageService;

    @ApiOperation("获取通知")
    @GetMapping("/auth/all")
    public Result<List<Message>> all(HttpServletRequest request){
        String userName = request.getHeader("userName");
        List<Message> list = messageService.list(new LambdaQueryWrapper<Message>().eq(Message::getAcceptorName, userName));
        return Result.common(list);
    }

    /**
     * 前端定时调用，有未读通知则返回true
     *
     * @param request 请求
     * @return {@link Result}<{@link Boolean}>
     */
    @ApiOperation("提示通知")
    @GetMapping("/auth/alert")
    public Result<Boolean> alert(HttpServletRequest request){
        String userName = request.getHeader("userName");
        boolean alert = messageService.alert(userName);
        return Result.common(alert);
    }

    @ApiOperation("标为已读")
    @PutMapping("/read/{id}")
    public Result<Boolean> read(@PathVariable("id") Integer id){
        boolean update = messageService.update(new LambdaUpdateWrapper<Message>().set(Message::getStatus, 1).eq(Message::getId, id));
        return Result.common(update);
    }

    @ApiOperation("删除通知")
    @DeleteMapping("/del/{id}")
    public Result<Boolean> del(@PathVariable("id") Integer id){
        boolean del = messageService.removeById(id);
        return Result.common(del);
    }
}

